/*
 * @Author: qin
 * @Date:   2018-08-28
 * +----------------------------------------------------------------------
 * | admin [ 后台管理系统公共配置信息 ]
 * | 计算内容区域高度，需传入头部class,内容区域class/id
 * +----------------------------------------------------------------------
 */
layui.define(['jquery','configure','configureCom','form'], function (exports) {
    var $ = layui.jquery;
    var configure = layui.configure;
    var configureCom = layui.configureCom;
    var form = layui.form;
    var timeFun = {
        init: function (titleClass, content) {
            //工程项目类型统计
            var timeData = configureCom.timeType();//现在的时间
            var nowYear = timeData.year;
            var nowMonth = timeData.month;
            var nowQuarter = timeData.quarter;
            var yearOption = '<option value="">请选择</option>';
            for (var y = 2018; y <= nowYear; y++) {
                if (y == configure.INIT_YEAR) {
                    yearOption += '<option value="' + y + '" selected>' + y + '年</option>';
                } else {
                    yearOption += '<option value="' + y + '">' + y + '年</option>';
                }
            }
            $('.yearSelect').html(yearOption);

            dateFun($('.yearSelect'), nowYear);
            form.render();

            form.on('select(yearSelect)',function(data){
                var year = data.value;
                var that = $(data.elem);
                dateFun(that, year);
            });

            function dateFun(that, year) {
                var monthHtml = '';
                var quarterHtml = '';
                if (year == nowYear) {
                    monthHtml = monthFun(nowMonth);
                    quarterHtml = quarterFun(nowQuarter);
                } else {
                    monthHtml = monthFun(12);
                    quarterHtml = quarterFun(4);
                }
                $('.timeSelectBox').find('.monthSelect').html(monthHtml);
                $('.timeSelectBox').find('.quarterSelect').html(quarterHtml);
                form.render('select');
            }

            //监听事件类型选择
            form.on('select(timeType)', function(data){
                var type = $(data.elem).val();
                var year = 0;
                var that = $(data.elem);
                if ($('.timeSelectBox').find('.yearSelect').val()) {
                    year = $('.timeSelectBox').find('.yearSelect').val();
                } else {
                    year = configure.INIT_YEAR;
                }
                dateFun(that, year);
                if (type == 1) {
                    $('.timeSelectBox').find('.quarterSelectBox').hide().find('.quarterSelect').val('');
                    $('.timeSelectBox').find('.monthSelectBox').hide().find('.monthSelectBox').val('');
                } else if (type == 2) {
                    $('.timeSelectBox').find('.quarterSelectBox').show();
                    $('.timeSelectBox').find('.monthSelectBox').hide().find('.monthSelectBox').val('');
                } else if (type == 3) {
                    $('.timeSelectBox').find('.quarterSelectBox').hide().find('.quarterSelectBox').val('');
                    $('.timeSelectBox').find('.monthSelectBox').show();
                }
            });

            // 月份方法
            function monthFun(month) {
                var option = '<option value="">请选择</option>';
                for (var i = 1; i <= month; i++) {
                    if (nowMonth == month) {
                        option += '<option value="' + i + '" selected>' + i + '月</option>';
                    } else {
                        option += '<option value="' + i + '">' + i + '月</option>';
                    }
                }
                return option;
            }

            //季度方法
            function quarterFun(quarter) {
                var option = '<option value="">请选择</option>';
                for (var i = 1; i <= quarter; i++) {
                    option += '<option value="' + i + '">' + i + '季度</option>';
                }
                return option;
            }
        }
    };
    exports('timeFun', timeFun);
});
